/* eslint-disable no-shadow */
/* eslint-disable prefer-destructuring */
/* eslint-disable no-useless-escape */
/* eslint-disable no-restricted-syntax */
import pcUploadImg from './pcUploadImg';
import wxUploadImg from './wxUploadImg';

const weChatInfo = navigator.userAgent.match(/MicroMessenger\/([\d\.]+)/i);
const appInfo = navigator.userAgent.match(/WptMessenger\/([\d\.]+)/i);

export default function uploadImage(callback, imgLimit, isCut, openRemote, files, addMD5) {
    let getQr = false;

    if (imgLimit && typeof imgLimit === 'object') {
        const option = imgLimit;
        imgLimit = option.imgLimit;
        isCut = option.isCut;
        openRemote = option.openRemote;
        files = option.files;
        addMD5 = option.addMD5;
        getQr = option.getQr;
    }

    // 暂时下线识别二位逻辑，因为目前同步任务可能会导致主线程卡顿时间非常长，等待web worker版本上线
    getQr = false;

    // 如果imgLimit小于1，则可上传9张图片
    imgLimit = Number(imgLimit) || 9;

    let uploadImg = pcUploadImg;
    if (appInfo && !isCut && WPT && WPT.uploadImg) {

        // app 并且有图片上传
        uploadImg = (callback, imgLimit) => {
            if (WPT.uploadImg) {
                WPT.uploadImg({
                    success(imgs = []) {
                        const serverIds = [];
                        const localIds = [];
                        const fileInfos = [];

                        let date = new Date();
                        if (WPT && WPT.nowTime) {
                            date = new Date(WPT.nowTime * 1000);
                        }
                        const year = date.getFullYear();
                        const month = (date.getMonth() + 1).toString();
                        const day = date.getDate();

                        // eslint-disable-next-line no-unused-vars
                        for (const key in imgs) {
                            if (!imgs[key].serverId || imgs[key].serverId.indexOf('null') != -1) {
                                // eslint-disable-next-line no-continue
                                continue;
                            }
                            let img = imgs[key].serverId;
                            let imgWH = img.match(/(\-W\d+H\d+)$/);
                            if (imgWH) {
                                imgWH = imgWH[1];
                            } else {
                                imgWH = '';
                            }
                            img = img.replace(/(\-W\d+H\d+)$/, '').substr(-36);
                            imgs[key].serverId = year + (month > 9 ? month : `0${month}`) + (day > 9 ? day : `0${day}`) + img + imgWH;
                            serverIds.push(imgs[key].serverId);

                            if (imgs[key].qrcode) {
                                fileInfos.push({
                                    name: imgs[key].serverId,
                                    code: imgs[key].qrcode,
                                });
                            }

                            const WHReg = imgs[key].serverId.match(/W(\d+)H(\d+)/);
                            if (WHReg) {
                                localIds.push(`${imgs[key].localId}#W${WHReg[1]}H${WHReg[2]}`);
                            } else {
                                localIds.push(imgs[key].localId);
                            }
                        }
                        callback(serverIds, localIds, fileInfos);
                    },
                    count: imgLimit,
                    isQr: getQr,
                });
            }
        };
    } else if ($.os.phone && weChatInfo && weChatInfo[1] > '6.0.2' && !isCut) {
        uploadImg = wxUploadImg;
    }
    uploadImg(callback, imgLimit, isCut, openRemote, files, addMD5, getQr);
}
